<template>
    <div>
        <a-modal
         v-model:visible="visible" 
         :title="'（'+formState.fgjry+'）变更岗位'" 
         @ok="handleOk" 
         @cancel="visible = false"
         class="enterprise-add-edit-modal"
         :footer="null"
        >
            <div v-if="visible" style="margin-bottom: 80px;">
                <a-form :form="form" layout="horizontal" class="add-edit-form">
                    <a-form-item label="变更岗位">
                        <a-select
                            ref="select"
                            placeholder="请选择变更岗位"
                            v-model:value="formState.fbghgw"
                            v-decorator="[
                            'fbghgw', { initialValue:formState.fbghgw, rules: rules.fbghgw}
                        ]"
                            >
                            <a-select-option v-for="(item,index) in rylxList" :value="item">{{ item }}</a-select-option>
                        </a-select>
                    </a-form-item>
                </a-form>
            </div>
            <div style="
                display: flex; justify-content: flex-end;
                margin-top: 20px; width: 100%;
                position: absolute; left: 0;
                bottom: 0; padding: 18px 24px;
                background-color: #fff;">
                <a-button @click="visible = false">取消</a-button>
                <a-button v-if="!formState.view" style="margin: 0 20px;" class="blue-btn" type="primary" @click="handleOk(true)">确定</a-button>
            </div>
        </a-modal>
    </div>
</template>
<script>
import compSelector from '@/components/compSelector/compSelector.vue'
import { queryYwzdByCode,spcyQyInfoqueryGjryWjml,
    spcyQyInfoUploadMoreTypeFile, 
    getFileTypeServe, 
    deleteFileById, 
    getCrossLocalFile,
    spcyQyInfoaddRygwbgjl,
 } from './service/index'
import moment from 'moment'
import YchConstant from "@/api/YchConstant";
import blobType from '@/common/blobType'
export default {
    components:{
        compSelector,
    },
    props:{

    },
    data(){
        return{
            visible:false,
            formState:{},
            form: this.$form.createForm(this, { name: 'coordinated' }),
            rules:{
                fbghgw: [{
                    required: true,
                    message: '请选择变更岗位',
                    trigger: 'blur',
                }],
            },
            rylxList:[],
            fjxxList:[],
        }
    },
    watch:{

    },
    mounted(){
        queryYwzdByCode().then(res=>{
            if(res.code == 200){
                this.rylxList = res.result
            }else{
                this.$message.warning(res.message)
            }
        })
    },
    methods:{
        getData(val){
            this.formState = {}
            this.formState = val
            
        },
        getFile(val){
            spcyQyInfoqueryGjryWjml({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.fjxxList = res.result
                }else{
                    this.$message.warning(res.message)
                }
            })
        },
        deleteClick(val){
            var that = this
            this.$confirm({
                title:'您确认要删除吗？',
                // icon: createVNode(ExclamationCircleOutlined),
                onOk() {
                    deleteFileById({id:val.id}).then(res=>{
                        if(res.code == 200){
                            that.$message.success('删除成功')
                            that.getFile(that.formState)
                        }else{
                            that.$message.warning(res.message)
                        }
                    })
                },
                
            });
        },
        viewClick(val){
            this.$refs.serverEditingViewRef.getData({id:val.id,fwjlx:val.fwjlx,type:'企业信息'})
            this.$refs.serverEditingViewRef.serverEditingVisible = true
        },
        lookOverClick(val){
            window.open(
                bootConfig[process.env.NODE_ENV].locality_ApI+YchConstant.SPZX_SPCY+'/spcySqsxWjjl/getCrossLocalFile?id='+val.id
            )
        },
        downloadClick(val){
            var that = this
            var fileName = val.fwjxsmc
            let param = {id:val.id}
            getFileTypeServe({id:val.id}).then(res=>{
                if(res.code == 200){
                    getCrossLocalFile(param).then((data) => {
                        if (!data) {
                            that.$message.warning('文件下载失败')
                            return
                        }else{
                        // if (typeof window.navigator.msSaveBlob !== 'undefined') {
                        //     window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }))
                        // } else {
                            let url = window.URL.createObjectURL(new Blob([data], { type: blobType.type[blobType.suffix.indexOf(val.fwjlx)] }))
                            let link = document.createElement('a')
                            link.style.display = 'none'
                            link.href = url
                            link.setAttribute('download', fileName + res.result)
                            document.body.appendChild(link)
                            link.click()
                            document.body.removeChild(link) //下载完成移除元素
                            window.URL.revokeObjectURL(url) //释放掉blob对象
                        // }
                        }
                    })
                }
                
            })
        },
        enterpriseBeforeUpload(e){
            return false
        },
        uploadSave(){
            if(!this.formState.id){
                this.handleOk(true,true)
            }
        },
        enterpriseUpload({file},val,type){//
            var that = this
            this.uploadLoading = true
            if(type == '照片'){
                if(file.name.indexOf('.png') == -1 || file.name.indexOf('.jpg') == -1 || file.name.indexOf('.jpeg') == -1){
                    this.$message.warning('请上传图片（.png、.jpg和.jpeg）')
                    return false
                }

            }
            if(typeof file.uid !== 'undefined') {
                // if(file.name.indexOf(".xls") !== -1 || file.name.indexOf(".xlsx") !==-1) {
                    const fileData = new FormData();
                    fileData.append("file",file);
                    fileData.append("id",this.formState.id);
                    fileData.append("fwjlx1",val.wjml);
                    
                    spcyQyInfoUploadMoreTypeFile(fileData).then(res => {
                        if(res.code){
                            if(res.code == 200){
                                this.uploadLoading = false
                                that.$message.success(res.message)
                            }else{
                                this.uploadLoading = false
                                that.$message.warning(res.message)
                            }
                            
                        }
                        if(res.byteLength){
                            if(res.code && res.code !== 200){
                                this.uploadLoading = false
                                that.$message.warning('请上传对应文件类型或者联系管理员!')
                            }else{
                                this.uploadLoading = false
                                that.$message.success('上传成功')
                            }
                        }
                        this.getFile(this.formState)
                    })
                // }
                
            }
        },
        handleOk(val){
            this.form.validateFields((err, values) => {
                if (!err) {
                    spcyQyInfoaddRygwbgjl({id:this.formState.id,fbghgw:values.fbghgw}).then(res=>{
                        if(res.code == 200){
                            this.$message.success(res.message)
                            this.$emit('KeyPersonSX')
                            this.visible = false
                        }else{
                            this.$message.warning(res.message)
                        }
                    })
                }
            })
        },
    }
}
</script>
<style lang="less">
.enterprise-add-edit-modal{
    .add-edit-form{
        display: flex;
        flex-wrap: wrap;
        .ant-form-item{
            display: flex;
        }
        .ant-form-item-label{
            width: 85px;
        }
        .ant-form-item-control-wrapper{
            width: 180px;
        }
        .flex-1-box{
            width: 100%;
            .ant-form-item-control-wrapper{
                flex: 1;
            }
        }
    }
}
</style>